//
//  MPBidTableCell.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/6.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPFilterConditionCell.h"
#import "MPFilterConditionItem.h"
#import "UIImage+Common.h"
#import "MPCellButton.h"

@interface MPFilterConditionCell ()
@property (nonatomic, strong)UILabel* titleLabel;
@property (nonatomic, strong)UIImageView* bottomSeperatorLineImageView;

@end

@implementation MPFilterConditionCell

+ (CGFloat)rowHeightForObject:(id)object
{
    return 44.0f;
}

//+ (CGFloat)estimateRowsHeight
//{
//    return 44.0f;
//}

+ (BOOL)shouldUseAutoLayout
{
    return NO;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType = UITableViewCellAccessoryNone;
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.selectBtn];
        [self.contentView addSubview:self.bottomSeperatorLineImageView];
        
        [self setupConstraints];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)prepareForReuse
{
    [super prepareForReuse];
}

- (void)setupConstraints
{
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.trailing.mas_equalTo(self);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.contentView).with.offset(10.0f);
    }];

    [self.bottomSeperatorLineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.bottom.mas_equalTo(self.contentView);
        make.leading.mas_equalTo(self.titleLabel);
        make.height.mas_equalTo(@(1.0f));
    }];
    
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.trailing.mas_equalTo(self.contentView).with.offset(-10.0f);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)setObject:(id)object
{
    [super setObject:object];
    
    if (object == nil)
    {
        return ;
    }
    else
    {
        if ([object isKindOfClass:[MPFilterConditionItem class]]) {
            MPFilterConditionItem* item = (MPFilterConditionItem *)object;
            if (![NSString stringIsEmpty:item.title]) {
                self.titleLabel.text = item.title;
            }
            
            if (item.isSelected) {
                [self.selectBtn setImage:[UIImage imageForKey:@"subscribe_icon_check_focused"] forState:UIControlStateNormal];
            }
            else
            {
                [self.selectBtn setImage:[UIImage imageForKey:@"subscribe_icon_check_default"] forState:UIControlStateNormal];
            }
        }
    }
}


#pragma mark - property

- (UIImageView *)bottomSeperatorLineImageView
{
    if (!_bottomSeperatorLineImageView) {
        _bottomSeperatorLineImageView = [[UIImageView alloc] initWithImage:[UIImage imageFromColor:[UIColor colorWithHexString:kCellBorderColor]]];
    }
    return _bottomSeperatorLineImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.textColor = [UIColor colorWithHexString:kContentTextColor];
        _titleLabel.font = [UIFont systemFontOfSize:kContentFontSize];
    }
    return _titleLabel;
}

- (MPCellButton *)selectBtn
{
    if (!_selectBtn) {
        _selectBtn = [MPCellButton buttonWithType:UIButtonTypeCustom];
    }
    return _selectBtn;
}

#pragma mark - KVO

- (void)objectPropertyChanged:(NSString *)property {
    if([property isEqualToString:@"isSelected"]){
        if ([self.object isKindOfClass:[MPFilterConditionItem class]]) {
            MPFilterConditionItem* item = (MPFilterConditionItem *)self.object;
            if (item.isSelected) {
                [self.selectBtn setImage:[UIImage imageForKey:@"subscribe_icon_check_focused"] forState:UIControlStateNormal];
            }
            else
            {
                [self.selectBtn setImage:[UIImage imageForKey:@"subscribe_icon_check_default"] forState:UIControlStateNormal];
            }
        }
    }
}

- (void)startObserveObjectProperty {
    [self addObservedProperty:@"isSelected"];
}

- (void)finishObserveObjectProperty {
    [self removeObservedProperty:@"isSelected"];
}

@end
